home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
gwuada_9.zip
/
MISC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-27
|
18KB
|
792 lines
/*
* Copyright (C) 1985-1992 New York University
*
* This file is part of the Ada/Ed-C system. See the Ada/Ed README file for
* warranty (none) and distribution info and also the GNU General Public
* License for more details.
*/
/* misc.c - miscellaneous programs */
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "time.h"
#include "ifile.h"
#include "miscp.h"
#ifdef BSD
#include <strings.h>
#endif
#ifndef LIBDIR
#define LIBDIR "/usr/local/lib"
#endif
#ifdef ADALIB
#define EXIT_INTERNAL_ERROR
#endif
#ifdef BINDER
#define EXIT_INTERNAL_ERROR
extern int adacomp_option;
#endif
#ifdef INT
#define EXIT_INTERNAL_ERROR
#endif
#ifndef EXPORT
#undef EXIT_INTERNAL_ERROR
#endif
#ifdef BSD
#include <sys/file.h>
#endif
char *LIBRARY_PREFIX= "";
/* PREDEFNAME gives directory path to predef files.
* libset() is used to toggle between libraries (the users and predef).
* tname = libset(lname) sets library prefix for ifopen, etc. to lname
* and returns prior setting in tname.
*/
static void openerr(char *filename, char *mode);
#ifdef SMALLOC
unsigned int smalloc_free = 0;
char *smalloc_ptr;
#define SMALLOC_BLOCK 2000
char **smalloc_table = (char **)0;
unsigned smalloc_blocks = 0;
#endif
char *smalloc(unsigned n) /*;smalloc*/
{
/* variant of malloc for use for blocks that will never be freed,
* primarily blocks used for small strings. This permits allocation
* in larger blocks avoiding the malloc overhead required for each block.
*/
#ifndef SMALLOC
return emalloct(n, "smalloc");
#else
char *p;
if (n & 1) n+= 1;
#ifdef ALIGN4
if (n & 2) n+= 2;
#endif
if (n > SMALLOC_BLOCK) { /* large block allocated separately */
#ifdef DEBUG
printf("smalloc: warning block %u exceeds %d SMALLOC_BLOCK\n",
n, SMALLOC_BLOCK);
#endif
p = emalloct(n, "smalloc");
return p;
}
if (n > smalloc_free) {
smalloc_ptr = emalloct(SMALLOC_BLOCK, "smalloc-block");
smalloc_free = SMALLOC_BLOCK;
smalloc_blocks++;
if (smalloc_blocks == 1) {
smalloc_table = (char **) emalloct(sizeof (char **),
"smalloc-table");
}
else { /* reallocate blocks */
smalloc_table = (char **) erealloct((char *)smalloc_table,
sizeof(char **) * (smalloc_blocks), "smalloc-table-realloc");
}
smalloc_table[smalloc_blocks-1] = smalloc_ptr;
}
p = smalloc_ptr;
smalloc_ptr += n;
smalloc_free -= n;
return p;
#endif
}
#ifdef DEBUG
void smalloc_list()
{
int i;
char **st;
st = smalloc_table;
for (i = 0; i < smalloc_blocks; i++) {
printf("%d %ld %x\n", i, *st, *st);
st++;
}
}
#endif
int is_smalloc_block(char *p) /*;is_smalloc_block*/
{
/* returns TRUE is p points within block allocated by smalloc */
#ifdef SMALLOC
int i;
char **st;
st = smalloc_table;
if (smalloc_blocks == 0) chaos("is_malloc_block - no blocks");
for (i = 0; i < smalloc_blocks; i++) {
if (*st <= p && p < (*st+(SMALLOC_BLOCK-1)))
return TRUE;
st++;
}
return FALSE;
#else
return FALSE;
#endif
}
void capacity(char *s) /*;capacity*/
{
/* called when compiler capacity limit exceeded.
* EXIT_INTERNAL_ERROR is defined when the module is run by itself
* (not spawned from adacomp) and DEBUG is not defined.
*/
#ifdef EXIT_INTERNAL_ERROR
fprintf(stderr, "capacity limit exceeded: %s\n", s);
exitp(RC_INTERNAL_ERROR);
#else
#ifdef DEBUG
printf("capacity limit exceeded: %s\nexecution abandoned \n", s);
#endif
fprintf(stderr, "capacity limit exceeded: %s\n", s);
exitp(RC_INTERNAL_ERROR);
#endif
}
#ifdef CHAOS
void chaos(char *s) /*;chaos*/
{
/* called when internal logic error detected and it is not meaningful
* to continue execution. This is never defined for the export version.
*/
fprintf(stderr, "chaos: %s\nexecution abandoned \n", s);
printf("chaos: %s\nexecution abandoned \n", s);
exitp(RC_INTERNAL_ERROR);
}
#else
void exit_internal_error() /*;exit_internal_error*/
{
/* called when internal logic error detected and it is not meaningful
* to continue execution. This procedure is called by the export version.
* EXIT_INTERNAL_ERROR is defined when the module is run by itself
* (not spawned from adacomp) and EXPORT is defined.
* Now that adabind is a separate module which can be called by itself
* or spawned from adacomp, we must test the run time flag adacomp_option
* to determine which case it is.
*/
#ifdef EXIT_INTERNAL_ERROR
#ifdef BINDER
if (adacomp_option)
#endif
fprintf(stderr, "Adaed internal error - Please report.\n");
exit(RC_INTERNAL_ERROR);
#else
exit(RC_INTERNAL_ERROR);
#endif
}
#endif
void exitp(int n) /*;exitp*/
{
/* synonym for exit() used so can trap exit() calls with debugger */
exit(n);
}
char *ecalloc(unsigned nelem, unsigned nsize) /*;ecalloc */
{
/* calloc with error check if no more */
char *p;
if (nelem > 20000) chaos("ecalloc: ridiculous argument");
p = calloc (nelem, nsize);
if (p == (char *) 0)
capacity("out of memory \n");
return p;
}
char *emalloc(unsigned n) /*;emalloc */
{ /* avoid BUGS - use calloc which presets result to zero ds 3 dec 84*/
/* malloc with error check if no more */
char *p;
if (n > 50000) chaos("emalloc: ridiculous argument");
p = calloc (1, n);
if (p == (char *) 0)
capacity("out of memory \n");
return (p);
}
char *erealloc(char *ptr, unsigned size) /*;eralloc */
{
/* realloc with error check if no more */
char *p;
p = realloc (ptr, size);
if (p == (char *) 0)
capacity("erealloc: out of memory \n");
return (p);
}
char *strjoin(char *s1, char *s2) /*;strjoin */
{
/* return string obtained by concatenating argument strings
* watch for either argument being (char *)0 and treat this as null string
*/
char *s;
if (s1 == (char *)0) s1= "";
if (s2 == (char *)0) s2 = "";
s = smalloc((unsigned) strlen(s1) + strlen(s2) + 1);
strcpy(s, s1);
strcat(s, s2);
return s;
}
int streq(char *a, char *b) /*;streq*/
{
/* test two strings for equality, allowing for null pointers */
if (a == (char *)0 && b == (char *)0)
return TRUE;
else if (a == (char *)0 || b == (char *)0)
return FALSE;
else return (strcmp(a, b) == 0);
}
char *substr(char *s, int i, int j) /*;substr */
{
/* return substring s(i..j) if defined, else return null ptr*/
int n;
char *ts, *t;
if (s == (char *)0) return (char *) 0;
n = strlen(s);
if (!(i > 0 && j <= n && i <= j)) return (char *)0;
/* allocate result, including null byte at end */
ts = smalloc((unsigned) j - i + 2);
t = ts;
s = s + (i - 1); /* point to start of source*/
for (; i <= j; i++) *t++ = *s++; /* copy characters */
*t = '\0'; /* terminate result */
return ts;
}
/* getopt(3) procedure obtained from usenet */
/*
* getopt - get option letter from argv
*/
#ifdef IBM_PC
#define nogetopt
#endif
#ifdef nogetopt
char *optarg; /* Global argument pointer. */
int optind = 0; /* Global argv index. */
static char *scan = NULL; /* Private scan pointer. */
int getopt(int argc, char **argv, char *optstring) /*;getopt */
{
register char c;
register char *place;
optarg = NULL;
if (scan == NULL || *scan == '\0') {
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
return (EOF);
if (strcmp (argv[optind], "--") == 0) {
optind++;
return (EOF);
}
scan = argv[optind] + 1;
optind++;
}
c = *scan++;
place = strchr (optstring, c);
if (place == NULL || c == ':') {
fprintf (stderr, "%s: unknown option -%c\n", argv[0], c);
return ('?');
}
place++;
if (*place == ':') {
if (*scan != '\0') {
optarg = scan;
scan = NULL;
}
else {
optarg = argv[optind];
optind++;
}
}
return (c);
}
#endif
char *greentime(int un) /*;greentime*/
{
/* get greenwich time in string of 23 characters.
* format of result is as follows
* 1984 10 02 16 30 36 nnn
* 123456789a123456789b123
* year mo da hr mi se uni
*
* greenwich time is used to avoid problems with daylight savings time.
* The last three characters are the compilation unit number
* (left filled with zeros if necessary).
* NOTE: changed to use local time to give approx. same time as
* SETL versi